package com.flyduck.vueshop.common.core.lang;

import lombok.Data;

import java.io.Serializable;

/**
 * @program: vueshop
 * @description:
 * @author: flyduck
 * @create: 2024-05-07 21:01
 **/
@Data
public class Result<T> implements Serializable {

    // 结果状态（200成功，400异常）
    private Integer code;

    //消息
    private String message;

    //结果数据（为了让服务之间调用知道data的类型，所以使用泛型）
    private T data;

    /**
     * 操作成功
     */
    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(200);
        result.setMessage("操作成功");
        result.setData(data);
        return result;
    }

    /**
     * 操作失败
     */
    public static <T> Result<T> fail(String message) {
        Result<T> result = new Result<>();
        result.setCode(400);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> fail(int code,String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        return result;
    }
}
